package controller

import (
	"gitee.com/kordar/bucket/form"
	"gitee.com/kordar/bucket/service"
	"github.com/gin-gonic/gin"
	"github.com/kordar/ginsys/util"
	response "github.com/kordar/goframework_resp"
	logger "github.com/kordar/gologger"
	"net/http"
	"path"
)

type BucketController struct {
	service service.BucketUploaderService
}

func NewBucketController(service service.BucketUploaderService) *BucketController {
	return &BucketController{service: service}
}

func (b *BucketController) Tree(ctx *gin.Context) {
	f := form.BucketListForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	response.Success(ctx, "", map[string]interface{}{
		"data": b.service.Tree(int64(f.ID), f.Dir, f.Next, int(f.Page), 10000, 0, 20, true, true),
	})
}

func (b *BucketController) List(ctx *gin.Context) {
	f := form.BucketListForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	list, _ := b.service.List(int64(f.ID), f.Dir, f.Next, int(f.Page), int(f.PageSize), false)

	count := b.service.Count(int64(f.ID), f.Dir)

	response.Data(ctx, "", list, int64(count))
}

func (b *BucketController) Del(ctx *gin.Context) {
	f := form.BucketDelForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	if f.FileName != "" {
		err := b.service.Del(int64(f.ID), f.FileName)
		if err != nil {
			response.Error(ctx, err, nil)
			return
		}
	}

	if f.Dir != "" {
		b.service.DelAll(int64(f.ID), f.Dir)
	}

	response.Success(ctx, "", nil)
}

func (b *BucketController) Download(ctx *gin.Context) {
	f := form.BucketDownloadForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	content, err := b.service.Download(int64(f.ID), f.FileName)
	if err != nil {
		response.Error(ctx, err, nil)
		return
	}

	// 设置头信息，告诉浏览器是附件下载
	ctx.Header("Content-Disposition", "attachment; filename="+f.Name)
	ctx.Data(http.StatusOK, "application/octet-stream", content)
}

func (b *BucketController) Rename(ctx *gin.Context) {
	f := form.BucketRenameForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	dir := path.Dir(f.FileName)
	err := b.service.Rename(int64(f.ID), path.Join(dir, f.NewFilename), f.FileName)
	if err != nil {
		response.Error(ctx, err, nil)
		return
	}

	response.Success(ctx, "", nil)
}

func (b *BucketController) Cut(ctx *gin.Context) {
	f := form.BucketCopyForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	for _, filename := range f.Files {
		base := path.Base(filename)
		err := b.service.Move(int64(f.ID), path.Join(f.Target, base), filename)
		if err != nil {
			logger.Warnf("cut file %s error: %s", filename, err)
			response.Error(ctx, err, nil)
			return
		}
	}

	response.Success(ctx, "", nil)
}

func (b *BucketController) Copy(ctx *gin.Context) {
	f := form.BucketCopyForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	for _, filename := range f.Files {
		base := path.Base(filename)
		err := b.service.Copy(int64(f.ID), path.Join(f.Target, base), filename)
		if err != nil {
			logger.Warnf("cut file %s error: %s", filename, err)
			response.Error(ctx, err, nil)
			return
		}
	}

	response.Success(ctx, "", nil)
}

func (b *BucketController) Upload(ctx *gin.Context) {
	f := form.BucketUploadForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		response.Error(ctx, err, nil)
		return
	}

	fileHeader, err := ctx.FormFile("file")
	if err != nil {
		return
	}

	err = b.service.Upload(int64(f.ID), f.Target, fileHeader)
	if err != nil {
		response.Error(ctx, err, nil)
		return
	}

	response.Success(ctx, "", nil)
}
